package util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @fileName: SightRegex
 * @author: Han
 * @date: 2018/5/24 11:41
 * @dscription:
 */
public class SightRegex {

    /**
     * 解析携程的评论
     *
     * @param pageStr 页面内容
     * @param pageNum 页码
     * @return 格式化的评论
     */
    public List<Map<String, String>> cTrip(String pageStr, int pageNum) {

        //切割携程评论成每条评论一条记录
        List<String> discussList = Regex.splitByRegex(pageStr, "http://schema.org/Review");
        //去掉第一个不包含评论的记录
        discussList.remove(0);

        List<Map<String, String>> discussMapList = new ArrayList<>();
        for (String discuss : discussList) {
            discuss = discuss.replace("&#x0A;", "");
            discuss = discuss.replace("&#x20;", "");
            Map<String, String> discussMap = extractCTripDiscuss(discuss);
            discussMap.put("当前页数", pageNum + "");
            discussMapList.add(discussMap);
        }
        return discussMapList;
    }

    /**
     * 解析携程的评论数据
     *
     * @param discuss 评论
     * @return 格式化的评论
     */
    private Map<String, String> extractCTripDiscuss(String discuss) {

        Map<String, String> map = new LinkedHashMap<>();
        String author = "-";
        String star = "-";
        String sight = "-";
        String fun = "-";
        String cost = "-";
        String disc = "-";
        String help = "-";
        String timeStamp = "-";

        String[] discussArr = discuss.split("\n");
        for (int i = 0; i < discussArr.length; i++) {
            try {
                String dis = discussArr[i];
                if (dis.contains("a itemprop=\"author\" ")) {
                    //用户名
                    int start = dis.indexOf("title=\"") + 7;
                    int end = dis.indexOf("\" target");
                    author = dis.substring(start, end);
                } else if (dis.contains("<!--调用星级个数,新增meta-->")) {
                    //评分
                    //找到标志,取下一行
                    dis = discussArr[i + 1];
                    int start = dis.indexOf(":") + 1;
                    int end = dis.indexOf(";");
                    star = dis.substring(start, end);
                    if ("20%".equals(star)) {
                        star = "1星";
                    } else if ("40%".equals(star)) {
                        star = "2星";
                    } else if ("60%".equals(star)) {
                        star = "3星";
                    } else if ("80%".equals(star)) {
                        star = "4星";
                    } else if ("100%".equals(star)) {
                        star = "5星";
                    }
                } else if (dis.contains("<span class=\"sblockline\">")) {
                    //景色,趣味,性价比
                    //景色
                    dis = discussArr[i + 1];
                    int start = dis.indexOf("：") + 1;
                    int end = dis.indexOf("&");
                    sight = dis.substring(start, end);
                    //趣味
                    dis = discussArr[i + 2];
                    start = dis.indexOf("：") + 1;
                    end = dis.indexOf("&");
                    fun = dis.substring(start, end);
                    //性价比
                    dis = discussArr[i + 3];
                    start = dis.indexOf("：") + 1;
                    end = dis.indexOf("&");
                    cost = dis.substring(start, end);
                } else if (dis.contains("<li itemprop=\"description\" class=\"main_con\">")) {
                    //评论
                    int start = dis.indexOf("heightbox") + 11;
                    int end = dis.indexOf("</span>");
                    disc = dis.substring(start, end);
                } else if (dis.contains("<span class=\"useful\">")) {
                    //有用数
                    int start = dis.indexOf("<em>") + 4;
                    int end = dis.indexOf("</em>");
                    help = dis.substring(start, end);
                } else if (dis.contains("<em itemprop=\"datePublished\">")) {
                    int start = dis.indexOf("datePublished") + 15;
                    int end = dis.indexOf("</em>");
                    timeStamp = dis.substring(start, end);
                }
            } catch (Exception e) {
                System.out.println(e.getMessage() + ",用户:" + author);
            }
        }
        map.put("来源", "携程");
        map.put("用户", author);
        map.put("评分", star);
        map.put("景色", sight);
        map.put("趣味", fun);
        map.put("性价比", cost);
        map.put("有用数", help);
        map.put("时间", timeStamp);
        map.put("评论", disc);
        return map;
    }

    public List<Map<String, String>> dianping(String pageContentStr, int pageNum) {
        List<Map<String, String>> list = new ArrayList<>();

        List<String> discussList = Regex.splitByRegex(pageContentStr, "<div class=\"main-review\">");
        discussList.remove(0);

        for (String dis : discussList) {
            dis = dis.replace("&nbsp;","");
            Map<String, String> disMap = extractDianpingDiscuss(dis);
            disMap.put("当前页数", pageNum + "");
            list.add(disMap);
        }

        return list;
    }

    private Map<String, String> extractDianpingDiscuss(String discuss) {
        Map<String, String> map = new LinkedHashMap<>();
        String author = "-";
        String authorRank = "-";
        String isVip = "-";
        String star = "-";
        String disc = "-";
        String follow = "-";
        String timeStamp = "-";

        String[] disArr = discuss.split("\n");
        for (int i = 0; i < disArr.length; i++) {
            try {
                String dis = disArr[i];
                if (dis.contains("<a class=\"name\"")) {
                    //用户
                    author = disArr[i + 8].replace(" ", "").replace("\t", "");
                } else if (dis.contains("user-rank-rst")) {
                    //用户等级
                    int start = dis.indexOf("user-rank-rst") + 14;
                    int end = dis.lastIndexOf(" rank");
                    authorRank = dis.substring(start, end);
                    if ("urr-rank10".equals(authorRank)) {
                        authorRank = "1级";
                    } else if ("urr-rank20".equals(authorRank)) {
                        authorRank = "2级";
                    } else if ("urr-rank30".equals(authorRank)) {
                        authorRank = "3级";
                    } else if ("urr-rank40".equals(authorRank)) {
                        authorRank = "4级";
                    } else if ("urr-rank45".equals(authorRank)) {
                        authorRank = "5级";
                    } else if ("urr-rank50".equals(authorRank)) {
                        authorRank = "6级";
                    } else if ("urr-rank60".equals(authorRank)) {
                        authorRank = "7级";
                    } else if ("urr-rank70".equals(authorRank)) {
                        authorRank = "8级";
                    } else if ("urr-rank80".equals(authorRank)) {
                        authorRank = "9级";
                    }
                    //是否是vip
                    dis = disArr[i + 1];
                    if (dis.contains("vip-gray")) {
                        isVip = "VIP";
                    } else {
                        isVip = "非VIP";
                    }
                } else if (dis.contains("sml-rank-stars")) {
                    int start = dis.indexOf("sml-rank-stars") + 15;
                    int end = dis.lastIndexOf(" star");
                    star = dis.substring(start, end);
                    if ("sml-str10".equals(star)) {
                        star = "1星";
                    } else if ("sml-str20".equals(star)) {
                        star = "2星";
                    } else if ("sml-str30".equals(star)) {
                        star = "3星";
                    } else if ("sml-str40".equals(star)) {
                        star = "4星";
                    } else if ("sml-str50".equals(star)) {
                        star = "5星";
                    }
                } else if (dis.contains("review-words")) {
                    disc = disArr[i + 1].replace(" ", "").replace("\t", "");
                } else if (dis.contains("<span class=\"time\">")) {
                    timeStamp = disArr[i + 1].replace("  ", "").replace("\t", "");
                } else if (dis.contains("data-send")) {
                    dis = disArr[i + 4];
                    int start = dis.indexOf("(") + 1;
                    int end = dis.indexOf(")");
                    if (start == -1 || end == -1) {
                        follow = "0";
                    } else {
                        follow = dis.substring(start, end);
                    }
                }
            } catch (Exception e) {
                System.err.println(e.getMessage() + ",author:" + author);
            }
        }

        map.put("来源", "大众点评");
        map.put("用户", author);
        map.put("用户等级", authorRank);
        map.put("VIP", isVip);
        map.put("评分", star);
        map.put("赞", follow);
        map.put("时间", timeStamp);
        map.put("评论", disc);

        return map;
    }
}
